home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / PATHOPEN.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  2KB  |  54 lines

  1. /*  002  14-Feb-87  pathopen.c
  2.  
  3.         Pathopen will open a file somewhere along the PATH.
  4.  
  5.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include "strmem.h"
  10.  
  11.  
  12. FILE *
  13. pathopen(fn,mode)      /* open a file somewhere along the path */
  14. char *fn;
  15. char *mode;
  16. {
  17.    FILE *fp;
  18.    int lastch;
  19.    register char *path, *sp;
  20.    char *psave, fname[100], *strchr(), *getenv();
  21.  
  22.    if ((fp = fopen(fn,mode)) == NULL)          /* try to open in current dir */
  23.  
  24.       if (path = getenv("PATH")) {             /* dizdn't open, any PATH? */
  25.          path = psave = Strdup(path);          /* need zapable copy of PATH */
  26.  
  27.          while (strlen(path)) {                /* while something to check... */
  28.  
  29.             if (sp = strchr(path,';'))         /* ; seperates dir names in */
  30.                *sp = '\0';                     /*  PATH, only chk 1 at a time */
  31.  
  32.             strcpy(fname,path);                /* build file name to chk for */
  33.  
  34.             if ((lastch = fname[strlen(fname)-1]) != '\\' && lastch != '/' &&
  35.                  lastch != ':')
  36.                strcat(fname,"\\");             /* add \ if not already a dir */
  37.  
  38.             strcat(fname,fn);                  /* add help name to dir name */
  39.  
  40.             if (fp = fopen(fname,mode))        /* we're done if it opened */
  41.                break;
  42.  
  43.             if (sp)                    /* if there was a ; there might be */
  44.                path = sp + 1;          /*   another dir in the path to chk */
  45.             else
  46.                break;                  /*   otherwise, just give up */
  47.          }
  48.  
  49.          free(psave);                  /* clean up after ourselves */
  50.       }
  51.  
  52.    return(fp);                 /* return whatever we got */
  53. }
  54.